package com.xzl.time.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xzl.time.config.ApplicationContextProvider;
import com.xzl.time.netty.MyWebSocketHandler;
import com.xzl.time.utils.MyEnum;
import com.xzl.tool.util.AssertUtil;
import com.xzl.tool.util.RespBean;
import io.netty.channel.Channel;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @describe ：退出登录断开与websocket的连接
 * @PageLocation:
 * @Auther xzl
 * @Date 2022/1/7 9:52
 */
@RestController
public class logOutController {

    // 送Spring容器中获取消息监听器容器,处理订阅消息sysNotice
    SimpleMessageListenerContainer sysNoticeContainer =
            (SimpleMessageListenerContainer) ApplicationContextProvider
                    .getApplicationContext()
                    .getBean("sysNoticeContainer");
    // 送Spring容器中获取消息监听器容器,处理点赞消息userNotice
    SimpleMessageListenerContainer userNoticeContainer =
            (SimpleMessageListenerContainer) ApplicationContextProvider
                    .getApplicationContext()
                    .getBean("userNoticeContainer");

    @GetMapping("/logout")
    private RespBean logout(String userName){
        AssertUtil.isTrue(StringUtils.isBlank(userName),"参数异常！");
        //拼接获取文章发布队列名称
        String queueName = MyEnum.ARTICLE_SUBSCRIBE_QUEUE_.getValue() + userName;
        //拼接获取文章点赞队列名称
        String userQueueName = MyEnum.ARTICLE_THUMBSUP.getValue() + userName;
        ConcurrentHashMap<String, Channel> userChannelMap = MyWebSocketHandler.userChannelMap;
        //移除对该用户的websocke的连接
        userChannelMap.remove(userName);
        //移除对该队列的监听
        sysNoticeContainer.removeQueueNames(queueName);
        userNoticeContainer.removeQueueNames(userQueueName);
        return RespBean.success();
    }
}
